qx.Class.define("caraffa.forms.ChartNumerosGrales",
{
	extend : qx.ui.form.ow.FormTab,

	construct : function ()
	{
		this.base(arguments, "ChartNumerosGrales", "Est: Numeros Generales");
		
// RPC
		this.rpcNum = new qx.io.remote.Rpc();
		this.rpcNum.setTimeout(10000);
		this.rpcNum.setUrl("services/");
		this.rpcNum.setServiceName("ChartNumerosGrales");
		try{
			this.cargaForm = this.rpcNum.callSync("cargaForm");
			this.resObras = this.cargaForm.resObras;
			this.resMedicos = this.cargaForm.resMedicos;
		}
		catch (ex)
		{	
			alert(ex);
		}		
		
		
		this.grpFiltros = new qx.ui.groupbox.GroupBox();
  		this.grpFiltros.setWidth(700);
  		this.grpFiltros.setLayout(new qx.ui.layout.Basic());
  		
  		this.txtFechaDesde = new qx.ui.form.ow.DateText("Fec. Desde:");
  		this.txtFechaHasta = new qx.ui.form.ow.DateText("Fec. Hasta:");
  		this.cmbObraSocial = new qx.ui.form.ow.ComboBox("Obra Social:",this.resObras,true);
  		this.cmbPlan = new qx.ui.form.ow.ComboFilter("Plan:");
  		this.cmbPlan.setEnabled(false);
  		var resLugar = [{label:"CONSULTORIO", value:"co"},{label:"CIRUGIA",value:"ci"}]
  		this.cmbLugar = new qx.ui.form.ow.ComboBox("Lugar",resLugar,true);
 		this.cmbLugar.setValue("co");
 		this.cmbLugar.setEnabled(false);
  		var resConsul = [{label:"CONSULTA", value:"co"},{label:"PRACTICA",value:"pr"}]
  		this.cmbMedico = new qx.ui.form.ow.ComboFilter("Medico:",this.resMedicos,true);
  		this.btnExecute = new qx.ui.form.Button("Generar Consulta");
  		this.btnExecute.setEnabled(false);
  		this.txtTotal = new qx.ui.form.ow.TextArea("Totales:");
		
  		this.grpFiltros.add(this.txtFechaDesde,{left:10, top:10});
  		this.grpFiltros.add(this.txtFechaHasta,{left:350, top:10});
  		this.grpFiltros.add(this.cmbObraSocial,{left:10, top:40});
  		this.grpFiltros.add(this.cmbPlan,{left:350, top:40});
  		this.grpFiltros.add(this.cmbLugar,{left:10, top:70});
  		this.grpFiltros.add(this.cmbMedico,{left:350, top:70});
  		this.grpFiltros.add(this.btnExecute,{left:550, top:120});
  		this.grpFiltros.add(this.txtTotal,{left:350, top:150});
		this._attachEvents();
		
		this.add(this.grpFiltros, {left:10, top:10});
		
	},
	members :
	{
		_attachEvents : function (){
			this.addListener('appear', this._initForm, this);
			this.cmbObraSocial.addListener('changeValue',this._obraChange,this);
			this.btnExecute.addListener('execute',this._getTotales,this);
		},
		_initForm : function(){			
		},		
		_obraChange: function(){
			this.cmbPlan.setValue("");
			if(this.cmbObraSocial.getValue() != ""){
				this.cmbPlan.setEnabled(true);
				this.btnExecute.setEnabled(true);
				try{
					var parPlan = new Object();
					parPlan.cod_obra_social = this.cmbObraSocial.getValue();
					this.cargaPlan = this.rpcNum.callSync("cargaPlan", parPlan);
					this.resPlan = this.cargaPlan;
					this.cmbPlan.setNewValues(this.resPlan);
				}
				catch (ex)
				{	
					alert(ex);
				}		
				
				
			}
			else{
				this.cmbPlan.setEnabled(false);
				this.btnExecute.setEnabled(false);
			}
		},
		_getTotales: function(){
			try{
				var parTot = new Object();
				parTot.fecha_desde = this.txtFechaDesde.getValueToSQL();
				parTot.fecha_hasta = this.txtFechaHasta.getValueToSQL();
				parTot.cod_obra_social = this.cmbObraSocial.getValue();
				parTot.cod_plan = this.cmbPlan.getValue();
				parTot.lugar = this.cmbLugar.getValue();
				parTot.dni_usuario = this.cmbMedico.getValue();
				this.getTotales = this.rpcNum.callSync("getTotales", parTot);
				this.txtTotal.setValue(
					"SubTotal de Consulta: " + this.getTotales.consulta + "\n" + "SubTotal de Prácticas: " + this.getTotales.practica + "\n" + "Total: " + this.getTotales.total  
				);
			}
			catch (ex)
			{	
				alert(ex);
			}					
		}
	}
});
